package com.ehais.modbuswifi;


import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ListView;

import com.ehais.adapter.NewScanBatteryAdapter;
import com.ehais.analysis.Plumbic;
import com.ehais.model.NewScanBatteryData;
import com.ehais.model.StationInfo;
import com.ehais.model.StationInfoResult;
import com.google.gson.Gson;

import java.util.List;
import java.util.Timer;
import com.ehais.modbuswifi.R;

public class MainActivityActivity extends AppCompatActivity implements View.OnClickListener {

    private StationInfo stationInfo = null;
    private ListView batteryListView;
    private String cagegory = "plumbic"; // plumbic   phosphoric
    private Button btnOpenAcid; // 开启铅酸/铁锂检测
    private Button btnCloseAcid;

    private Timer timer;
    private Integer timerIndex;//
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_activity);
        batteryListView = (ListView) findViewById(R.id.batteryListView);

        String json = "{\"msg\":\"操作成功\",\"code\":200,\"data\":{\"id\":25771,\"deptName\":\"江岸区\",\"deptId\":10101,\"location\":\"基站\",\"stationName\":\"黄石盟山村\",\"stationCode\":\"101012500000000001\",\"operationId\":\"3\",\"dischargeWay\":\"假负载\",\"stationType\":\"机房\",\"batteryCategory\":\"铅酸\",\"electricityIn\":\"单相引入\",\"runningStatus\":\"正常\",\"monitorStatus\":\"正常\",\"modelCount\":\"4\",\"modelCapacity\":\"25\",\"fcVoltage\":\"53.5\",\"jcVoltage\":\"56.4\",\"lowVoltageFirst\":\"46\",\"lowVoltageSecond\":\"44\",\"setGroupCapacity\":\"150\",\"setGroupCount\":\"2\",\"groupCount\":\"2\",\"longitude\":\"113.859273\",\"latitude\":\"28.010063\",\"loadCurrent\":\"44\",\"feedbackBatteryLife\":\"88\",\"sourceOfProblem\":\"1\",\"originalStationName\":null,\"originalStationCode\":null,\"isCheck\":\"Y\",\"batteryLists\":[{\"id\":496318,\"identificationCode\":\"\",\"batteryName\":\"第1组蓄电池组\",\"capacity\":\"150\",\"monomerNumber\":\"24\",\"manufacturerId\":null,\"manufacturer\":\"江苏理士电池有限公司\",\"model\":\"2V\",\"manufacturerDate\":\"2023-07-31\",\"stationId\":25771},{\"id\":496319,\"identificationCode\":\"\",\"batteryName\":\"第2组蓄电池组\",\"capacity\":\"150\",\"monomerNumber\":\"24\",\"manufacturerId\":null,\"manufacturer\":\"江苏理士电池有限公司\",\"model\":\"2V\",\"manufacturerDate\":\"2023-08-03\",\"stationId\":25771}]}}";
        String data = "0000000000670104640BD90BD50A780A7C0A800A770A680A760A8B0A860A920A850A880A8F0A840A8A0A8E0A840A8A0AA10A740A860A830A820A8A0A900A830A8B0A860A710A7A0A850AA10AA90AAA0A9A0A950A8C0A7A0A830A880A7C0A730A740A7A0A840A980A8E0A800A9C";

        Gson gson = new Gson();
        StationInfoResult stationResult = gson.fromJson(json, StationInfoResult.class);
        stationInfo = stationResult.getData();
        Plumbic.getInstance().init();
        List<NewScanBatteryData> newScanBatteryData = Plumbic.getInstance().success(stationInfo,data);

        NewScanBatteryAdapter adapter = new NewScanBatteryAdapter(MainActivityActivity.this,R.layout.adapter_new_scan_battery,newScanBatteryData);
        batteryListView.setAdapter(adapter);
        ViewGroup.LayoutParams params = batteryListView.getLayoutParams();
        batteryListView.setLayoutParams(params);
        batteryListView.requestLayout();

        btnOpenAcid = (Button) findViewById(R.id.btnOpenAcid);
        btnCloseAcid = (Button) findViewById(R.id.btnCloseAcid);
        btnOpenAcid.setOnClickListener(this);
        btnCloseAcid.setOnClickListener(this);
        if(cagegory.equals("plumbic")) btnOpenAcid.setText("开始采集铅酸电池数据");
        if(cagegory.equals("phosphoric")) btnOpenAcid.setText("开始采集铁锂电池数据");
    }


    @Override
    public void onClick(View v) {
        if(v.getId() == R.id.btnOpenAcid){
            this.onStartPlumbicOrPhosphoric();
        }else if(v.getId() == R.id.btnCloseAcid){
            this.onClosePlumbicOrPhosphoric();
        }
//        switch(v.getId()){
//            case R.id.btnOpenAcid:
//                break;
//            case R.id.btnCloseAcid:
//            default:
//                break;
//        }
    }

    //开始采集数据按钮
    public void onStartPlumbicOrPhosphoric(){
        this.btnOpenAcid.setVisibility(View.GONE);
        this.btnCloseAcid.setVisibility(View.VISIBLE);
        if(this.cagegory.equals("plumbic")){

        }else if(this.cagegory.equals("phosphoric")){

        }
    }

    //关闭采集数据按钮
    public void onClosePlumbicOrPhosphoric(){
        this.btnOpenAcid.setVisibility(View.VISIBLE);
        this.btnCloseAcid.setVisibility(View.GONE);
    }
}